package com.xinruke.hostels.reservation.service.pms;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.cache.util.SysCodeUtil;
import com.xinruke.hostels.common.cache.util.SysSqlParamUtil;
import com.xinruke.hostels.common.constants.RedisConstants;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.exception.ArgumentException;
import com.xinruke.hostels.common.exception.BizException;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YewuyonghuCaozuoRizhi;
import com.xinruke.hostels.common.po.YwFangtairiqijilu;
import com.xinruke.hostels.common.po.YwYudingDingdan;
import com.xinruke.hostels.common.po.YwYudingDingdanZixiang;
import com.xinruke.hostels.common.po.constants.YwFangtairiqijiluConstants;
import com.xinruke.hostels.common.po.constants.YwYudingDingdanConstants;
import com.xinruke.hostels.common.service.CustomerService;
import com.xinruke.hostels.common.service.OperateLogService;
import com.xinruke.hostels.common.util.CustomSecurity;
import com.xinruke.hostels.common.util.DateCalendarUtil;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.reservation.interfaces.dto.DiscountsReservationQueryDTO;
import com.xinruke.hostels.reservation.interfaces.dto.pms.*;
import com.xinruke.hostels.reservation.interfaces.vo.BookerInfoVO;
import com.xinruke.hostels.reservation.interfaces.vo.RoomInfoVO;
import com.xinruke.hostels.reservation.mapper.YwFangjianleixingMapper;
import com.xinruke.hostels.reservation.mapper.YwFangtairiqijiluMapper;
import com.xinruke.hostels.reservation.mapper.YwYudingDingdanMapper;
import com.xinruke.hostels.reservation.mapper.YwYudingDingdanZixiangMapper;
import com.xinruke.hostels.reservation.service.DiscountsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BookingService
 * @CreateDate 2019/7/5
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 预订Service
 */
@Service
@Slf4j
public class BookingService {
    @Autowired
    YwYudingDingdanMapper yudingDingdanMapper;

    @Autowired
    YwYudingDingdanZixiangMapper yudingDingdanZixiangMapper;

    @Autowired
    YwFangjianleixingMapper fangjianleixingMapper;

    @Autowired
    YwFangtairiqijiluMapper fangtairiqijiluMapper;

    @Autowired
    OperateLogService operateLogService;

    @Autowired
    ManageUserCache manageUserCache;

    @Resource
    private DiscountsService discountsService;

    @Resource(name = "commonRedisTemplate")
    RedisTemplate commonRedisTemplate;

    /**
     * 根据酒店ID获取预订集合
     *
     * @param bookingQueryDTO
     * @return
     */
    public RowsDataVO<BookingQueryResultDTO> getBookingListByHotelId(BookingQueryByHotelIdDTO bookingQueryDTO) {
        RowsDataVO<BookingQueryResultDTO> bookingQueryResultDTORowsDataVO = new RowsDataVO<>();

        QueryConditions queryConditions = bookingQueryDTO.getQueryConditions();
        queryConditions.addCondition("jiudianId", EConditionOperator.EQULS, bookingQueryDTO.getJiudianId());
        queryConditions.addCondition("tuanduimingcheng", EConditionOperator.EQULS, bookingQueryDTO.getTuanduimingcheng());
        queryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);

        if (!StringUtils.isEmpty(bookingQueryDTO.getFangxingId()) || !StringUtils.isEmpty(bookingQueryDTO.getFangjianhao())) {
            QueryConditions zixiangQueryConditions = new QueryConditions();
            zixiangQueryConditions.addCondition("fangxingId", EConditionOperator.EQULS, bookingQueryDTO.getFangxingId());
            zixiangQueryConditions.addCondition("fangjianhao", EConditionOperator.EQULS, bookingQueryDTO.getFangjianhao());
            zixiangQueryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
            MyExample zixiangMyExample = new MyExample(YwYudingDingdanZixiang.class, zixiangQueryConditions);
            List<YwYudingDingdanZixiang> zixiangList = yudingDingdanZixiangMapper.selectByExample(zixiangMyExample);
            if (null == zixiangList || zixiangList.size() == 0) {
                return new RowsDataVO<>();
            }
            List<Object> dingdanIdList = new ArrayList<>();
            for (YwYudingDingdanZixiang yudingDingdanZixiang : zixiangList) {
                dingdanIdList.add(yudingDingdanZixiang.getDingdanId());
            }
            queryConditions.addCondition("id", dingdanIdList);
        }

        MyExample myExample = new MyExample(YwYudingDingdan.class, queryConditions, bookingQueryDTO.getOrderFields());

        QueryConditions weekendQueryConditions = new QueryConditions();
        weekendQueryConditions.addCondition("yudingrendianhua", EConditionOperator.LIKE, bookingQueryDTO.getPhoneNameNo());
        weekendQueryConditions.addCondition("yudingrenxingming", EConditionOperator.LIKE, bookingQueryDTO.getYudingrenxingming());
        Weekend weekend = myExample.createOrWeekend(YwYudingDingdan.class, weekendQueryConditions);
        weekend.and(myExample.getOredCriteria().get(0));
        Page<YwYudingDingdan> page = PageHelper.startPage(bookingQueryDTO.getPageNo(), bookingQueryDTO.getPageSize());
        List<YwYudingDingdan> resultList = yudingDingdanMapper.selectByExample(weekend);

        List<BookingQueryResultDTO> dataList = resultList.stream().map(m -> {
            BookingQueryResultDTO bookingQueryResultDTO = new BookingQueryResultDTO();
            String jiudianMingcheng = SysSqlParamUtil.getSysParamDesc("JIUDIAN.MINGCHENG", m.getJiudianId());
            bookingQueryResultDTO.setJiudianMingcheng(jiudianMingcheng);
            BeanUtils.copyProperties(m, bookingQueryResultDTO);
            return bookingQueryResultDTO;
        }).collect(Collectors.toList());

        List<RoomInfoVO> roomVOList;
        for (BookingQueryResultDTO bookingQueryResultDTO : dataList) {
            QueryConditions fangjianQueryCondition = new QueryConditions();
            fangjianQueryCondition.addCondition("dingdanId", EConditionOperator.EQULS, bookingQueryResultDTO.getId());
            fangjianQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
            MyExample fangjianMyExample = new MyExample(YwYudingDingdanZixiang.class, fangjianQueryCondition);
            List<YwYudingDingdanZixiang> fangjianResultList = yudingDingdanZixiangMapper.selectByExample(fangjianMyExample);

            String fangjianInfo = "";
            roomVOList = new ArrayList<>();
            for (YwYudingDingdanZixiang yudingDingdanZixiang : fangjianResultList) {
                if (!StringUtils.isEmpty(fangjianInfo)) {
                    fangjianInfo = fangjianInfo + "| ";
                }
                fangjianInfo = fangjianInfo + yudingDingdanZixiang.getFangxingmingcheng() + "（" + yudingDingdanZixiang.getFangjianhao() + "）";

                RoomInfoVO roomInfoVO = new RoomInfoVO();
                roomInfoVO.setFangxingId(yudingDingdanZixiang.getFangxingId());
                roomInfoVO.setFangxingmingcheng(yudingDingdanZixiang.getFangxingmingcheng());
                roomInfoVO.setFangjianId(yudingDingdanZixiang.getFangjianId());
                roomInfoVO.setFangjianhao(yudingDingdanZixiang.getFangjianhao());
                roomInfoVO.setMendianjia(yudingDingdanZixiang.getMendianjia());
                roomInfoVO.setYouhuijia(yudingDingdanZixiang.getYouhuijia());
                roomInfoVO.setLikaishijian(yudingDingdanZixiang.getLikaishijian());
                roomInfoVO.setRuzhushijian(yudingDingdanZixiang.getRuzhushijian());
                roomVOList.add(roomInfoVO);
            }
            bookingQueryResultDTO.setRoomVOList(roomVOList);
            bookingQueryResultDTO.setFangjianInfo(fangjianInfo);
        }

        bookingQueryResultDTORowsDataVO.setPageNo(page.getPageNum());
        bookingQueryResultDTORowsDataVO.setTotalRecords(page.getTotal());
        bookingQueryResultDTORowsDataVO.setTotalPages(page.getPages());
        bookingQueryResultDTORowsDataVO.setRows(dataList);

        return bookingQueryResultDTORowsDataVO;
    }

    /**
     * 获取预订集合
     *
     * @param bookingQueryDTO
     * @return
     */
    public RowsDataVO<BookingQueryResultDTO> getBookingList(BookingQueryDTO bookingQueryDTO) {
        RowsDataVO<BookingQueryResultDTO> bookingQueryResultDTORowsDataVO = new RowsDataVO<>();

        QueryConditions queryConditions = bookingQueryDTO.getQueryConditions();
        queryConditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());
        queryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);

        if (!StringUtils.isEmpty(bookingQueryDTO.getFangxingId()) || !StringUtils.isEmpty(bookingQueryDTO.getFangjianhao())) {
            QueryConditions zixiangQueryConditions = new QueryConditions();
            zixiangQueryConditions.addCondition("fangxingId", EConditionOperator.EQULS, bookingQueryDTO.getFangxingId());
            zixiangQueryConditions.addCondition("fangjianhao", EConditionOperator.EQULS, bookingQueryDTO.getFangjianhao());
            zixiangQueryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
            MyExample zixiangMyExample = new MyExample(YwYudingDingdanZixiang.class, zixiangQueryConditions);
            List<YwYudingDingdanZixiang> zixiangList = yudingDingdanZixiangMapper.selectByExample(zixiangMyExample);
            if (zixiangList == null || zixiangList.size() == 0) {
                return bookingQueryResultDTORowsDataVO;
            }
            List<Object> dingdanIdList = new ArrayList<>();
            for (YwYudingDingdanZixiang yudingDingdanZixiang : zixiangList) {
                dingdanIdList.add(yudingDingdanZixiang.getDingdanId());
            }

            queryConditions.addCondition("id", dingdanIdList);
        }

        MyExample myExample = new MyExample(YwYudingDingdan.class, queryConditions, bookingQueryDTO.getOrderFields());

        QueryConditions weekendQueryConditions = new QueryConditions();
        if (!StringUtils.isEmpty(bookingQueryDTO.getPhoneNameNo())) {
            weekendQueryConditions.addCondition("yudingrendianhua", EConditionOperator.LIKE, bookingQueryDTO.getPhoneNameNo());
            weekendQueryConditions.addCondition("yudingrenxingming", EConditionOperator.LIKE, bookingQueryDTO.getPhoneNameNo());
            weekendQueryConditions.addCondition("zhengjianhao", EConditionOperator.LIKE, bookingQueryDTO.getPhoneNameNo());
        }
        Weekend weekend = myExample.createOrWeekend(YwYudingDingdan.class, weekendQueryConditions);
        weekend.and(myExample.getOredCriteria().get(0));

        Page<YwYudingDingdan> page = PageHelper.startPage(bookingQueryDTO.getPageNo(), bookingQueryDTO.getPageSize());
        List<YwYudingDingdan> resultList = yudingDingdanMapper.selectByExample(weekend);

        List<BookingQueryResultDTO> dataList = resultList.stream().map(m -> {
            BookingQueryResultDTO bookingQueryResultDTO = new BookingQueryResultDTO();
            BeanUtils.copyProperties(m, bookingQueryResultDTO);
            bookingQueryResultDTO.setZhengjianhao(CustomSecurity.decodeIDCard(bookingQueryResultDTO.getZhengjianhao()));
            bookingQueryResultDTO.setYudingrendianhua(CustomSecurity.decodeMobile(bookingQueryResultDTO.getYudingrendianhua()));
            return bookingQueryResultDTO;
        }).collect(Collectors.toList());

        List<RoomInfoVO> roomVOList;
        for (BookingQueryResultDTO bookingQueryResultDTO : dataList) {
            QueryConditions fangjianQueryCondition = new QueryConditions();
            fangjianQueryCondition.addCondition("dingdanId", EConditionOperator.EQULS, bookingQueryResultDTO.getId());
            fangjianQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
            MyExample fangjianMyExample = new MyExample(YwYudingDingdanZixiang.class, fangjianQueryCondition);
            List<YwYudingDingdanZixiang> fangjianResultList = yudingDingdanZixiangMapper.selectByExample(fangjianMyExample);

            String fangjianInfo = "";
            roomVOList = new ArrayList<>();
            for (YwYudingDingdanZixiang yudingDingdanZixiang : fangjianResultList) {
                if (!StringUtils.isEmpty(fangjianInfo)) {
                    fangjianInfo = fangjianInfo + "| ";
                }
                fangjianInfo = fangjianInfo + yudingDingdanZixiang.getFangxingmingcheng() + "（" + yudingDingdanZixiang.getFangjianhao() + "）";

                RoomInfoVO roomInfoVO = new RoomInfoVO();
                roomInfoVO.setFangxingId(yudingDingdanZixiang.getFangxingId());
                roomInfoVO.setFangxingmingcheng(yudingDingdanZixiang.getFangxingmingcheng());
                roomInfoVO.setFangjianId(yudingDingdanZixiang.getFangjianId());
                roomInfoVO.setFangjianhao(yudingDingdanZixiang.getFangjianhao());
                roomInfoVO.setMendianjia(yudingDingdanZixiang.getMendianjia());
                roomInfoVO.setYouhuijia(yudingDingdanZixiang.getYouhuijia());
                roomInfoVO.setLikaishijian(yudingDingdanZixiang.getLikaishijian());
                roomInfoVO.setRuzhushijian(yudingDingdanZixiang.getRuzhushijian());
                roomVOList.add(roomInfoVO);
            }
            bookingQueryResultDTO.setRoomVOList(roomVOList);
            bookingQueryResultDTO.setFangjianInfo(fangjianInfo);
        }

        bookingQueryResultDTORowsDataVO.setPageNo(page.getPageNum());
        bookingQueryResultDTORowsDataVO.setTotalRecords(page.getTotal());
        bookingQueryResultDTORowsDataVO.setTotalPages(page.getPages());
        bookingQueryResultDTORowsDataVO.setRows(dataList);

        return bookingQueryResultDTORowsDataVO;
    }

    /**
     * 获取预订详情
     *
     * @param bookingDetailQueryDTO
     * @return
     */
    public BookingDetailQueryResultDTO getBookingDetail(BookingDetailQueryDTO bookingDetailQueryDTO) {
        BookingDetailQueryResultDTO bookingDetailQueryResultDTO = new BookingDetailQueryResultDTO();
        int yudingId = bookingDetailQueryDTO.getYudingId();

        YwYudingDingdan yudingDingdan = yudingDingdanMapper.selectByPrimaryKey(yudingId);
        if (yudingDingdan == null) {
            return bookingDetailQueryResultDTO;
        }
        BeanUtils.copyProperties(yudingDingdan, bookingDetailQueryResultDTO);
        int ruzhutianshu = DateCalendarUtil.getNaturalDayDifferent(bookingDetailQueryResultDTO.getRuzhushijian(), bookingDetailQueryResultDTO.getLikaishijian());
        bookingDetailQueryResultDTO.setRuzhutianshu(ruzhutianshu);
        bookingDetailQueryResultDTO.setYudingrendianhua(CustomSecurity.decodeMobile(bookingDetailQueryResultDTO.getYudingrendianhua()));
        bookingDetailQueryResultDTO.setZhengjianhao(CustomSecurity.decodeIDCard(bookingDetailQueryResultDTO.getZhengjianhao()));

        QueryConditions fangjianQueryCondition = new QueryConditions();
        fangjianQueryCondition.addCondition("dingdanId", EConditionOperator.EQULS, yudingId);
        fangjianQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        MyExample fangjianMyExample = new MyExample(YwYudingDingdanZixiang.class, fangjianQueryCondition);
        List<YwYudingDingdanZixiang> fangjianResultList = yudingDingdanZixiangMapper.selectByExample(fangjianMyExample);

        List<RoomInfoVO> roomVOList = fangjianResultList.stream().map(m -> {
            RoomInfoVO roomInfoVO = new RoomInfoVO();
            roomInfoVO.setFangxingId(m.getFangxingId());
            roomInfoVO.setFangxingmingcheng(m.getFangxingmingcheng());
            roomInfoVO.setFangjianId(m.getFangjianId());
            roomInfoVO.setFangjianhao(m.getFangjianhao());
            roomInfoVO.setMendianjia(m.getMendianjia());
            roomInfoVO.setYouhuijia(m.getYouhuijia());
            roomInfoVO.setLikaishijian(m.getLikaishijian());
            roomInfoVO.setRuzhushijian(m.getRuzhushijian());

            List<BookerInfoVO> bookerInfoVOList = new ArrayList<>();
            BookerInfoVO bookerInfoVO = new BookerInfoVO();
            bookerInfoVO.setYudingxingming(m.getYudingxingming());
            bookerInfoVO.setLianxidianhua(CustomSecurity.decodeMobile(m.getLianxidianhua()));
            bookerInfoVO.setZhengjianleixing(m.getYudingrenzhengjianleixing());
            bookerInfoVO.setZhengjianleixingmingcheng(SysCodeUtil.getSysParamDesc(SysDicConstant.ZHENGJIANLEIXING.toUpperCase(), m.getYudingrenzhengjianleixing()));
            bookerInfoVO.setZhengjianhao(CustomSecurity.decodeIDCard(m.getYudingrenzhengjianhao()));
            bookerInfoVOList.add(bookerInfoVO);

            roomInfoVO.setBookerInfoVOList(bookerInfoVOList);
            return roomInfoVO;
        }).collect(Collectors.toList());

        bookingDetailQueryResultDTO.setRoomVOList(roomVOList);

        return bookingDetailQueryResultDTO;
    }


    /**
     * 取消预订
     * <p>
     * 1.Redis加锁
     * 2.修改订单状态
     * 3.解锁订单下房间
     * 4.写入操作日志
     *
     * @param bookingCancelDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<BookingCancelResultDTO> cancelBooking(BookingCancelDTO bookingCancelDTO) {
        String traceId = CustomSecurity.getTraceID();
        log.info("traceId[" + traceId + "]" + JSONObject.toJSONString(bookingCancelDTO));

        ResultVO<BookingCancelResultDTO> resultVO = ResultVO.fail();

        int jiudianId = manageUserCache.getJiudianId();
        String redisLockKey = RedisConstants.HOTEL_ROOM_LOCK_KEY_PREFIX + jiudianId;
        try {
            //使用redis，根据酒店id对房间操作加锁
            boolean lockRet = commonRedisTemplate.opsForValue().setIfAbsent(redisLockKey, jiudianId);
            if (!lockRet) {
                log.error("traceId[" + traceId + "]预订取消冲突，请重试");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultVO.setMessage("预订取消冲突，请重试");
                return resultVO;
            }

            String yudingId = bookingCancelDTO.getYudingid();
            QueryConditions dingdanQueryCondition = new QueryConditions();
            dingdanQueryCondition.addCondition("jiudianId", EConditionOperator.EQULS, jiudianId);
            dingdanQueryCondition.addCondition("id", EConditionOperator.EQULS, yudingId);
            dingdanQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
            MyExample dingdanMyExample = new MyExample(YwYudingDingdan.class, dingdanQueryCondition);
            List<YwYudingDingdan> yudingDingdanList = yudingDingdanMapper.selectByExample(dingdanMyExample);
            if (CollectionUtils.isEmpty(yudingDingdanList)) {
                log.error("traceId[" + traceId + "]预订信息为空");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultVO.setMessage("预订信息为空");
                return resultVO;
            }

            YwYudingDingdan yudingDingdan = yudingDingdanList.get(0);
            byte zhuangtai = yudingDingdan.getZhuangtai();
            if (zhuangtai != YwYudingDingdanConstants.ZHUANGTAI_YITIJIAO) {
                log.error("traceId[" + traceId + "]该订单不能取消");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultVO.setMessage("该订单不能取消");
                return resultVO;
            }

            //修改订单状态
            yudingDingdan.setZhuangtai(YwYudingDingdanConstants.ZHUANGTAI_QUXIAO);
            int dingdanRet = yudingDingdanMapper.updateByPrimaryKeySelective(yudingDingdan);
            if (dingdanRet <= 0) {
                log.error("traceId[" + traceId + "]修改订单状态失败");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultVO.setMessage("修改订单状态失败");
                return resultVO;
            }

            //获取该订单下房间集合
            QueryConditions fangjianQueryCondition = new QueryConditions();
            fangjianQueryCondition.addCondition("dingdanId", EConditionOperator.EQULS, yudingId);
            fangjianQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
            MyExample fangjianMyExample = new MyExample(YwYudingDingdanZixiang.class, fangjianQueryCondition);
            List<YwYudingDingdanZixiang> fangjianResultList = yudingDingdanZixiangMapper.selectByExample(fangjianMyExample);
            if (CollectionUtils.isEmpty(fangjianResultList)) {
                log.error("traceId[" + traceId + "]房间信息为空");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultVO.setMessage("房间信息为空");
                return resultVO;
            }

            //遍历房间，解锁
            for (YwYudingDingdanZixiang yudingDingdanZixiang : fangjianResultList) {
                QueryConditions fangtairiqijiluQueryCondition = new QueryConditions();
                fangtairiqijiluQueryCondition.addCondition("jiudianId", EConditionOperator.EQULS, yudingDingdanZixiang.getJiudianId());
                fangtairiqijiluQueryCondition.addCondition("fangjianId", EConditionOperator.EQULS, yudingDingdanZixiang.getFangjianId());
                fangtairiqijiluQueryCondition.addCondition("kaishishijian", EConditionOperator.EQULS, yudingDingdanZixiang.getRuzhushijian());
                fangtairiqijiluQueryCondition.addCondition("jieshushijian", EConditionOperator.EQULS, yudingDingdanZixiang.getLikaishijian());
                fangtairiqijiluQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
                fangtairiqijiluQueryCondition.addCondition("fangjianleibie", EConditionOperator.EQULS, YwFangtairiqijiluConstants.FANGJIANLEIBIE_FANGJIAN);
                MyExample fangtairiqijiluMyExample = new MyExample(YwFangtairiqijilu.class, fangtairiqijiluQueryCondition);
                YwFangtairiqijilu fangtairiqijilu = new YwFangtairiqijilu();
                fangtairiqijilu.setShanchubiaoshi(SysDicConstant.DELFLAG_1);

                int fangtaiRet = fangtairiqijiluMapper.updateByExampleSelective(fangtairiqijilu, fangtairiqijiluMyExample);
                if (fangtaiRet <= 0) {
                    log.error("traceId[" + traceId + "]修改房态失败");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultVO.setMessage("修改房态失败");
                    return resultVO;
                }
            }

            if (yudingDingdan.getLaiyuan() != null && yudingDingdan.getLaiyuan() == 1){
                List<DiscountsReservationQueryDTO> collect = fangjianResultList.stream().filter(e -> e.getShifouyouhui()).map(e -> {
                    DiscountsReservationQueryDTO queryDTO = new DiscountsReservationQueryDTO();
                    BeanUtils.copyProperties(e, queryDTO);
                    queryDTO.setZhengjianhao(e.getYudingrenzhengjianhao());
                    queryDTO.setZhengjianleixing(e.getYudingrenzhengjianleixing());
                    queryDTO.setHuiyuanId(e.getYudingrenshenfenId());
                    queryDTO.setJieshushijian(e.getLikaishijian());
                    return queryDTO;
                }).collect(Collectors.toList());
                ResultVO resultVO1 = discountsService.cancelReservation(collect);
                if (resultVO1.getCode() != ResultVO.SUCCESS) {
                    throw new ArgumentException("取消订单返回优惠次数失败");
                }
            }


            //写入操作日志
            YewuyonghuCaozuoRizhi yewuyonghuCaozuoRizhi = new YewuyonghuCaozuoRizhi("预订", "预订取消", "订单号[" + yudingDingdan.getDingdanhao() + "]");
            operateLogService.writeOperateLog(yewuyonghuCaozuoRizhi);

            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("预订取消成功");
        } catch (Exception e) {
            log.error("traceId[" + traceId + "]预订取消失败，系统异常", e);
            throw new BizException("预订取消失败，系统异常");
        } finally {
            commonRedisTemplate.delete(redisLockKey);
        }

        return resultVO;
    }

}
